package com.bymboy.emos.wx.config;

import cn.hutool.core.util.StrUtil;
import com.bymboy.emos.wx.db.dao.SysConfigDao;
import com.bymboy.emos.wx.db.pojo.SysConfig;
import com.bymboy.emos.wx.db.pojo.SystemConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @Package: com.bymboy.emos.wx.config
 * @ClassName: EMosConfig
 * @Author: My
 * @CreateTime: 2021/9/24 8:12
 * @Description:
 */
@ServletComponentScan
@SpringBootConfiguration
@EnableAsync
@Slf4j
public class EmosConfig {
    @Resource
    private SysConfigDao sysConfigDao;

    @Autowired
    private SystemConstants constants;

    @Value("${emos.image-folder}")
    private String imageFolder;

    @PostConstruct
    public void init() {
        log.info("imageFolder:" + imageFolder);
        boolean mkdirs = new File(imageFolder).mkdirs();
        log.info("mkdirs:" + mkdirs);

        log.info(constants.toString());
        List<SysConfig> list = sysConfigDao.selectAllParams();
        for (SysConfig one : list) {
            log.info(one.toString());
            String key = one.getParamKey();
            key = StrUtil.toCamelCase(key);
            String value = one.getParamValue();
            try {
                Field field = constants.getClass().getDeclaredField(key);
                field.set(constants, value);
            } catch (Exception e) {
                log.error("执行异常", e);
            }
        }
        log.info(constants.toString());
    }

}
